package gsp420;

public class Test implements Runnable
{
	/** print out one thing {@link #numTimes} number of times */
	String output;
	/** how many times to print out {@link #output} */
	int numTimes;
	
	Object mySemaphore;
	/**
	 * @param a_output see {@link #output}
	 * @param a_numTimes see {@link #numTimes}
	 */
	public Test(String a_output, int a_numTimes, Object semaphore){
		output = a_output;
		numTimes = a_numTimes;
		mySemaphore = semaphore;
	}
	
	void doTheThing(){
		for(int i = 0; i < numTimes; ++i){
			try{Thread.sleep(1);}catch(Exception e){}
			System.out.print(output);
		}
	}
	
	public void run(){
		if(mySemaphore!=null){
			synchronized(mySemaphore){
				doTheThing();
			}
		}else{
			doTheThing();
		}
	}
	
	public static void main(String[] args)
	{
		final int COUNT = 3;
		// "thread pool"
		Thread[] t = new Thread[COUNT];// Thread ** t = new Thread * [COUNT];
		Test[] tests = new Test[COUNT];// Thread ** tests = new Test * [COUNT];

		// if this is set to null, the output will be more like random static
		Object door = new Object();

		for(int i = 0; i < COUNT; ++i){
			String outText = "";
			outText += (char)('a'+i);
			tests[i] = new Test( outText, 20, door);
			t[i] = new Thread( tests[i] );
		}
		for(int i = 0; i < COUNT; ++i)
		{
			t[i].start();
		}
	}
}
